// pages/sales/radar/behavior.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
var app = getApp();
Page(Object.assign({}, baseHandle,{

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    list: [],
    total:"",
    isLoading: false,
    pageSize: 10,
    curPage: 1,
    isBottom: false,
    hideDateModal: true,
    btEndDate:"",
    defBtEndDate:"",
    btStartDate:"",
    defBtStartDate:"",
    mSDate: "",
    mEDate: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    var that = this;
    that.setData({
      bType: options.bType || "",
    });
    wx.setNavigationBarTitle({
      title: options.title || "",
    })
    that.setData({
      btEndDate: util.formatDate(new Date()),
      btStartDate: util.formatDate(new Date(Date.now() - (7 * 86400000))),
      defBtEndDate: util.formatDate(new Date()),
      defBtStartDate: util.formatDate(new Date(Date.now() - (7 * 86400000))),
    });
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      that.queryBehaviorTrail();
    });
  },
  queryBehaviorTrail(cb) {
    var ctx = this;
    if (ctx.data.isLoading || ctx.data.isBottom) {
      return;
    }
    // wx.showLoading({
    //   title: '加载中'
    // });
    ctx.setData({ isLoading: true });
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/bahavior/queryBehaviorByType",
      data: {
        type:ctx.data.bType,
        cusmallToken: cusmallToken,
        startDate: ctx.data.btStartDate,
        endDate: ctx.data.btEndDate,
        start: (ctx.data.curPage - 1) * ctx.data.pageSize,
        limit: ctx.data.pageSize
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          var list = data.model.list;
          for (let item of list) {
            item.behaviorTxt = util.behaviorType(item.type, item.count);
            item.behaviorTime = util.timestampToDayHkt(item.createTime);
            if (item.sttr1.length > 10) {
              item.sttr1 = item.sttr1.substring(0, 6) + "...";
            }
          }
          ctx.setData({
            list: ctx.data.list.concat(list),
            total: data.model.total
          });
          if (data.model.total == ctx.data.list.length) {
            ctx.setData({
              isBottom: true
            });
          }
        }
      },
      fail() {

      },
      complete() {
        ctx.setData({ isLoading: false });
        // wx.hideLoading();
      }
    });
  },
  defResetDate(){
    var ctx = this;
    ctx.setData({
      btEndDate: ctx.data.defBtEndDate,
      btStartDate: ctx.data.defBtStartDate,
      curPage: 1,
      list: [],
      isBottom: false
    });
    ctx.queryBehaviorTrail();
  },
  showDateModal() {
    var ctx = this;
    ctx.setData({
      hideDateModal: false,
      mSDate: ctx.data.btStartDate,
      mEDate: ctx.data.btEndDate,
    })
  },
  hideDateModal() {
    this.setData({
      hideDateModal: true
    })
  },
  bindDateChange(e) {
    console.log(e)
    var op = e.currentTarget.dataset.op;
    if ("start" == op){
      this.setData({
        mSDate: e.detail.value
      });
    }else if("end" == op){
      this.setData({
        mEDate: e.detail.value
      });
    }
  },
  fetchBehTra(e) {
    var ctx = this;
    ctx.handleCommonFormSubmit(e);
    ctx.setData({
      hideDateModal: false,
      btStartDate: ctx.data.mSDate,
      btEndDate: ctx.data.mEDate,
      curPage:1,
      list:[],
      isBottom:false
    });
    ctx.queryBehaviorTrail();
    ctx.hideDateModal();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let ctx = this;
    ctx.data.curPage = 1;
    ctx.data.list = [];
    ctx.queryBehaviorTrail();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let ctx = this;
    if (ctx.data.isLoading) {
      return;
    }
    ctx.setData({ curPage: ctx.data.curPage + 1 });
    ctx.queryBehaviorTrail();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))